/**
 * weapp-js v2.2.3(2022-4-26)
 * https://codeup.aliyun.com/motigo/motigo/weapp_base
 * Copyright@ 2022 Motigo, Inc.
 * 
 */
// weapp_base 框架自身网络的接口调用封装

class BaseRequest {
    constructor() {
        this.HOST = 'https://api.scf.moapp.net.cn/release/weapp_base';
        // this.HOST = 'https://dev.scf.mozigu.net/release/weapp_base';
        this.appid = '';
        this.openid = '';
        this.unionid = '';
        this.sessionId = '';
    }
    get(path, params, enableCache, expireIn) {
        return this.request('GET', path, params);
    }
    post(path, params) {
        return this.request('POST', path, params);
    }
    put(path, params) {
        return this.request('PUT', path, params);
    }
    delete(path, params) {
        return this.request('DELETE', path, params);
    }
    // 更新用户数据，由session.js调用（不太合理）
    updateSession(sessionId = '', appid = '', openid = '', unionid = '') {
        console.log('updateSession');
        console.log(sessionId);
        this.sessionId = sessionId;
        this.appid = appid;
        this.openid = openid;
        this.unionid = unionid;
    }
    request(method, path, params) {
        console.log('baseRequest.request');
        console.log(this.sessionId);
        return new Promise((resolve, reject) => {
            wx.request({
                method,
                timeout: 10000,
                header: {
                    'content-type': 'application/json',
                    appid: this.appid,
                    openid: this.openid,
                    unionid: this.unionid,
                    session: this.sessionId,
                },
                url: this.HOST + path,
                data: params,
                success(res) {
                    if (res.data.code == 0 || res.data.code === 'OK') {
                        resolve(res.data.data);
                    } else {
                        reject(res.data);
                    }
                },
                fail(err) {
                    reject(err);
                },
            });
        });
    }
}

export default new BaseRequest();
